package com.egoo.ticket.server.service.impl;

import com.egoo.ticket.server.dao.EwsRoleDao;
import com.egoo.ticket.server.pojo.entity.EwsRole;
import com.egoo.ticket.server.pojo.vo.InteractiveDataVo;
import com.egoo.ticket.server.service.RoleService;
import com.egoo.ticket.server.utils.common.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author：victor_tang
 * @Date ：2020/2/16
 * @Description：系统角色管理实现类
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class RoleServiceImpl implements RoleService {
	@Autowired
	EwsRoleDao ewsRoleDao;
	@Autowired
	CommonUtil commonUtil;
	/**
	 * TODO 根据不用条件获取角色列表信息
	 * @author: victor_tang
	 * @createtime: 2020/2/16 13:37
	 * @param searchmap 1
	 * @return: java.utils.List<com.egoo.ews.service.vo.EwsRole>
	 */
	@Override
	public List<EwsRole> getRolesListByCondition(Map<String, Object> searchmap) {
		return ewsRoleDao.selectRolesListByCondition(searchmap);
	}
	/**
	 * TODO 根据不用条件获取label/value列表信息
	 * @author: victor_tang
	 * @createtime: 2020/2/16 13:38
	 * @param searchMap 1
	 * @return: java.utils.List<com.egoo.ews.service.vo.InteractiveData>
	 */
	@Override
	public List<InteractiveDataVo> getRolesInteractiveByCondition(Map<String, Object> searchMap, String tenantId) {
		searchMap.put("tenantId",tenantId);
		return ewsRoleDao.selectRolesInteractiveByCondition(searchMap);
	}
	/**
	 * TODO 根据角色id获取角色信息
	 * @author: victor_tang
	 * @createtime: 2020/2/16 13:39
	 * @param id 1
	 * @return: com.egoo.ews.service.vo.EwsRole
	 */
	@Override
	public EwsRole selectById(String id,String tenantId) {
		return ewsRoleDao.selectByRoleId(id,tenantId);
	}
	/**
	 * TODO 同步角色信息
	 * @author: victor_tang
	 * @createtime: 2020/2/16 13:39
	 * @param ewsRoleList 1
	 * @return: java.lang.Long
	 */
	@Override
	public Long addRoleBatch(List<EwsRole> ewsRoleList) {
		log.info("新增角色同步数据为：" + ewsRoleList);
		String tenantId = ewsRoleList.get(0).getTenantId();
		ewsRoleList = ewsRoleList.stream().map(ewsRole -> ewsRole.setTenantId(tenantId)).collect(Collectors.toList());
		// 2.批量新增角色信息
		return ewsRoleDao.insertBatch(ewsRoleList);
	}
	/**
	 * TODO 批量更新角色信息
	 * @author: victor_tang
	 * @createtime: 2020/2/16 13:40
	 * @param ewsRoleList 1
	 * @return: java.lang.Long
	 */
	@Override
	public Long updateRoleBatch(List<EwsRole> ewsRoleList) {
		log.info("修改角色同步数据为：" + ewsRoleList);
		Long result = 0L;
		for (EwsRole ewsRole : ewsRoleList) {
			ewsRoleDao.update(ewsRole);
			result++;
		}
		return result;
	}

	/**
	 * TODO 根据用户ids获取,拼接的角色name字符串
	 * @author: victor_tang
	 * @createtime: 2020/2/16 13:40
	 * @param roleIds 1
	 * @param tenantId
	 * @return: java.lang.String
	 */
	@Override
	public String getNamesByRoleIds(List<String> roleIds, String tenantId) {
		if (roleIds == null || roleIds.size() == 0) {
			return null;
		}
		StringBuffer roleNames = new StringBuffer();
		Map<String, Object> searchmap = new HashMap<>(2);
		searchmap.put("roleIds", roleIds);
		searchmap.put("tenantId",tenantId);
		List<EwsRole> roleList =
				ewsRoleDao.selectRolesListByCondition(searchmap);
		for (EwsRole ewsRole : roleList) {
			roleNames.append(ewsRole.getName()).append(",");
		}
		// 删除末尾逗号
		if (roleNames != null && roleNames.length() > 0) {

			roleNames.deleteCharAt(roleNames.length() - 1);
			return roleNames.toString();
		}
		return null;
	}
}
